package com.flydog.taskManager.common.util;

import com.flydog.taskManager.common.api.ApiException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期相关工具类
 */
public class DateUtil {
    public static Date phaseDateString(String dataString) throws ApiException {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            return dateFormat.parse(dataString);
        } catch (ParseException e) {
            throw new ApiException(404, "日期格式错误");
        }
    }

    public static Calendar initDateToStartTimeInDay(String dateString) throws ApiException {
        Calendar initCalendar = Calendar.getInstance();
        initCalendar.setTime(phaseDateString(dateString));
        return initDateToStartTimeInDay(initCalendar);
    }

    public static Calendar initDateToEndTimeInDay(String dateString) throws ApiException {
        Calendar initCalendar = Calendar.getInstance();
        initCalendar.setTime(phaseDateString(dateString));
        return initDateToEndTimeInDay(initCalendar);
    }

    public static Calendar initDateToStartTimeInDay(Calendar initDate) {
        initDate.set(Calendar.HOUR_OF_DAY, 0);
        initDate.set(Calendar.SECOND, 0);
        initDate.set(Calendar.MINUTE, 0);
        initDate.set(Calendar.MILLISECOND, 0);
        return initDate;
    }

    public static Calendar initDateToEndTimeInDay(Calendar initDate) {
        initDate.set(Calendar.HOUR_OF_DAY, 23);
        initDate.set(Calendar.SECOND, 59);
        initDate.set(Calendar.MINUTE, 59);
        initDate.set(Calendar.MILLISECOND, 999);
        return initDate;
    }
}
